# 计算1～100000之间所有素数的个数， 要求如下:
# - 编写函数判断一个数字是否为素数，然后统计素数的个数；
# -对比1: 对比使用多进程和不使用多进程两种方法的统计速度。
# -对比2：对比开启4个多进程和开启10个多进程两种方法的速度。

import threading, math, time
from multiprocessing import Pool

def prime( num ):
    if num in [0,1]:
        return False
    for i in range( 2, int( math.sqrt(num) ) + 1 ):
        if num % i == 0:
            return False
    return True

print( 'common method' )
start = time.time()
prinumbers = [ i for i in range(1, 100000) if prime(i) ]
print( 'Number of prime :', len( prinumbers ) )
end = time.time()
total1 = end - start
print( 'Time of the process: ', total1 )


print( 'Multithreading' )
t = threading.Thread( target = prime )
# t2 = threading.Thread( target = prime )
start2 = time.time()
prinumbers = [ i for i in range(1, 10000) if prime(i) ]
print( 'Number of prime: ', len( prinumbers ) )
# thread.start_new_thread( i for i in range(1, 5000) if prime(i) )
# thread.start_new_thread( i for i in range(5000, 10000) if prime(i) )
end2 = time.time()
total2 = end2 - start2
print( '>>>>Time of the process: ', total2 )

if total1 < total2:
    print( 'Common method is faster' )
elif total1 == total2:
    print( 'same' )
else:
    print( 'Multithreading is faster' )


# if __name__ == '__main__':

#     #最多5个进程同时工作

#     with Pool(5) as p:

#         print(sum(p.map(prime, range(100000))))